<template>
    <div class="product_list" :style="{ 'height': productsHeight }">
        <div class="product_list_header">
            <div style="" class="product_list_serch-text">
                <van-search class="product_list_serch1" placeholder="请输入供应商名称" v-model="searchValue" shape="round"
                    clearable />
                <div class="product_list_serch2">
                    <div style="">
                        <van-button class="product_list-btn" round @click="onSearch">搜索</van-button>
                    </div>
                </div>
            </div>
            <div class="product_list_serch">
                <div class="product_list_serch_div">
                    <div class="product_list_serch_div_line" :style="{
                        transform: `translateX(${offset}vw)`,
                        transition: 'transform 0.5s ease-out',
                    }">
                        <div v-for="(item, index) in searchList" :style="{ 'margin-left': index == 0 ? '' : '2.6vw' }"
                            :class="serchListActive == index && 'product_list_serch_active'" :key="index"
                            class="product_list_serch_item" @click="handleSerchList(index, item.status)">
                            {{ item.name }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh" :style="{ 'height': refreshHeight }">
            <van-list v-model:loading="loading" :finished="finished" @load="onLoad" offset="50">
                <template v-slot:finished>
                    <div class="nothing_div" v-if="list.length == 0">
                        <van-image :src="nothingData"></van-image>
                        <div class="nothing_font">暂无数据</div>
                    </div>
                </template>
                <div class="product_list_van_list">
                    <div v-for="(item, index) in list" :key="index" class="product_list_van_list_div"
                        @click="handleGetDetails(item.id)">
                        <div class="div_right">
                            <div class="div_right_top">
                                <div class="style_name">
                                    {{ item.supplierName }}
                                </div>
                                <div class="div_right_top_img" v-if="item.bigBrandStatus == 1">免签</div>
                            </div>
                            <div class="div_right_center">
                                <div class="contract">
                                    <div :style="{
                                        color: item.sellStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.sellStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        供销合同</div>
                                </div>
                                <div class="contract_break">
                                    <div :style="{
                                        color: item.packageStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.packageStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        包销合同
                                    </div>
                                </div>
                                <div class="contract_break">
                                    <div :style="{
                                        color: item.depositStatus == '1' ? '#19D11B' : '#FC2540',
                                        backgroundColor: item.depositStatus == '1' ? '#E7FDEE' : '#FDE7E7',
                                    }">
                                        保证金协议
                                    </div>
                                </div>
                            </div>
                            <div class="div_right_bottom">
                                <div class="div_right_bottom_div">
                                    <div class="label">商品总数</div>
                                    <div class="value">{{ item.productsCount }}</div>
                                </div>
                                <div class="div_right_bottom_div">
                                    <div class="label">上架总数</div>
                                    <div class="value">{{ item.productsShopping }}</div>
                                </div>
                                <div class="div_right_bottom_div">
                                    <div class="label">选品数</div>
                                    <div class="value">{{ item.productsOpt }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </van-pull-refresh>

    </div>
</template>
<script setup lang="ts">
import { onMounted, ref, watch, onUnmounted } from "vue";
import { productSelectionList, getHmSupplierMyList, noProductSelectionList } from "@/api/supplierList/index";
import { useRouter } from "vue-router";

const router = useRouter();
const nothingData = new URL("@/assets/porduct/icon_zanwushuju.png", import.meta.url).href
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    supplierName: "",
    sellStatus: "",
});
const searchValue = ref("");
const searchList = ref([
    {
        name: "全部",
        index: 0,
        status: "",
    },
    {
        name: "已签合同",
        index: 1,
        status: "1",
    },
    {
        name: "未签合同",
        index: 2,
        status: "0",
    },
    {
        name: '已选品',
        index: 3,
        status: '2'
    },
    {
        name: '无选品',
        index: 4,
        status: '3'
    }
]);
const serchListActive = ref(0);
const refreshing = ref(false);
const loading = ref(false);
const finished = ref(false);
const list = ref<any[]>([]);
/** 滑动距离 */
const offset = ref(0);
const defaultPhoneHeight = ref(0) //屏幕默认高度
const nowPhoneHeight = ref(0) //屏幕现在的高度
const productsHeight = ref()
const refreshHeight = ref()
// 限制连续刷新数据重复问题
const refreFlag = ref(true)
// 限制快速切换数据乱序问题
const serchFlag = ref(true)
// 在组件挂载完成后添加事件监听器
onMounted(() => {
    defaultPhoneHeight.value = window.innerHeight;
    // 添加事件监听器
    window.addEventListener('resize', handleResize);
})
// 处理窗口大小变化的函数
const handleResize = () => {
    nowPhoneHeight.value = window.innerHeight;
};
// 在组件卸载前移除事件监听器
onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
});
const emits = defineEmits(['doShow'])
watch(nowPhoneHeight, () => {
    if (defaultPhoneHeight.value != nowPhoneHeight.value) {
        // 当软键盘弹出,改变bottomIsSHow变量然后去控制bottom的显示与隐藏
        emits('doShow', false)
        productsHeight.value = 'calc(100vh - 11.7vw)'
        refreshHeight.value = 'calc(100vh - 37.3vw)'
    } else {
        // 相等代表软键盘收起后,与上同理
        emits('doShow', true)
        productsHeight.value = 'calc(100vh - 25.0vw)'
        refreshHeight.value = 'calc(100vh - 50.6vw)'
    }
})

const onSearch = () => {
    queryParams.value.supplierName = searchValue.value;
    onRefresh();
};
const handleSerchList = async (index: number, status: string) => {
    if (serchFlag.value) {
        serchFlag.value = false
        serchListActive.value = index;
        queryParams.value.sellStatus = status;


        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        await onLoad(); // 重新加载数据
    }
};
/** 获取详情 */
const handleGetDetails = (id: string) => {
    router.push(`/researchSupplierDetail/${id}`);
};
/** 查询商品列表 */
const getList = async () => {
    serchFlag.value = false
    var res
    // 已选品
    if (serchListActive.value == 3) {
        res = await productSelectionList({ supplierName: queryParams.value.supplierName, pageNum: queryParams.value.pageNum, pageSize: queryParams.value.pageSize })
        // 未选品
    } else if (serchListActive.value == 4) {
        res = await noProductSelectionList({ supplierName: queryParams.value.supplierName, pageNum: queryParams.value.pageNum, pageSize: queryParams.value.pageSize })
    } else {
        res = await getHmSupplierMyList(queryParams.value);
    }
    setTimeout(() => {
        refreFlag.value = true
        // 空数据 停止加载
        if (res.rows.length == 0) {
            list.value = [];
            finished.value = true;
            serchFlag.value = true;
            return;
        }
        // 有数据 赋值列表 加载状态结束
        res.rows.forEach((item) => {
            finished.value = false;
            serchFlag.value = true;
            list.value.push(item);
        });
        loading.value = false;
        refreshing.value = false;
        serchFlag.value = true;
        // 如果list长度大于等于总数据条数，数据全部加载完成
        if (list.value.length >= res.total) {
            finished.value = true; //结束加载
        }
    }, 400)
};
/** 加载更多 */
const onLoad = async () => {
    getList();
    queryParams.value.pageNum++;
};
/** 下拉 */
const onRefresh = () => {
    if (refreFlag.value && serchFlag.value) {
        serchFlag.value = false
        refreFlag.value = false
        finished.value = false; // 清空列表数据
        refreshing.value = false;
        loading.value = true; // 加载状态
        queryParams.value.pageNum = 1; // 分页数赋值为1
        list.value = []; // 清空数组
        onLoad(); // 重新加载数据
    } else {
        finished.value = false;
        refreshing.value = false;
    }
};
</script>
<style lang="less" scoped>
.product_list {
    width: 100%;
    height: 100vh;
    background-color: #f6faff;

    .product_list_header {
        width: 375px;
        height: 150px;
        background: linear-gradient(#CDECFF 0%, #FFFFFF 100%);
        filter: blur(0px);
        margin-bottom: 10px;

        :deep(.van-search__field) {
            background-color: #fff;
            border-radius: 20px;
        }

        .product_list_serch-text {
            display: flex;
            align-items: center;
            width: 100%;
            height: 50px;
            position: relative;
            top: 60px;
        }

        :deep(.van-field__control) {
            padding-right: 64px;
        }

        .product_list_serch1 {
            display: flex;
            align-items: center;
            background: none;
            width: 100%;
            height: 35px;

            :deep(.van-search__content) {
                height: 35px;
                background-color: #fff;
            }

            :deep(.van-search__field) {
                height: 35px;
            }

            :deep(.van-field__clear) {
                right: 64px !important;
            }
        }

        .product_list_serch2 {
            position: absolute;
            right: 20px;

            :deep(.van-button__text) {
                font-size: 14px;
            }

            .product_list-btn {
                width: 54px;
                height: 26px;
                font-size: 14px;
                color: #ffffff;
                border: none;
                background: #0094ff;
                white-space: nowrap;
            }
        }
    }

    .nothing_div {
        width: 100%;
        margin: auto;
        text-align: center;
        margin-top: 20vh;
        font-weight: 400;
        font-size: 12px;
        color: #B8BECA;
        line-height: 12px;
        flex-wrap: wrap;

        .van-image {
            width: 160px;
            height: 160px;
        }

    }



    &_serch {
        display: flex;
        width: calc(100% - 20px);
        align-items: center;
        justify-content: space-between;
        padding: 0 10px;
        height: 40px;
        margin-bottom: 10px;
        position: relative;
        top: 60px;

        &_div {
            width: 100%;
            height: 40px;
            overflow: hidden;

            &_line {
                height: 40px;
                display: flex;
                width: 760px;
            }
        }

        :deep(.van-image) {
            width: 20px;
            height: 20px;
        }

        &_item {
            color: #1a1b1c;
            text-align: center;
            font-size: 12px;
            line-height: 40px;
            padding: 0 5px;
        }

        &_active {
            color: #0094ff;
            border-bottom: 3px solid #0094ff;
        }
    }

    :deep(.van-pull-refresh) {
        height: calc(100vh - 210px);
        overflow: scroll;
    }

    &_van_list {
        // min-height: calc(100vh - 140px);
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;

        &_div {
            width: 100%;
            height: 110px;
            box-sizing: border-box;
            background-color: #fff;
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 5px 15px;
            box-sizing: border-box;
            display: flex;
            justify-content: space-between;
        }

        .div_right {
            width: 100%;
            display: flex;
            flex-wrap: wrap;

            .div_right_top {
                height: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .style_name {
                    flex: 1;
                    min-width: 0;
                    font-size: 14px;
                    font-weight: bold;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }

                .div_right_top_img {
                    height: 100%;
                    background: linear-gradient(136deg, #E9D1A5 0%, #CEA46C 100%);
                    border-radius: 5px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    padding: 0 20px;
                    font-weight: 500;
                    font-size: 12px;
                    color: #1B1714;
                }

                .stats {
                    // width: 60px;
                    // height: 18px;
                    padding: 2px 4px;
                    line-height: 18px;
                    font-size: 12px;
                    // border: 1px solid #A3A3A3;
                    color: #a3a3a3;
                    text-align: center;
                    border-radius: 3px;
                }
            }

            .div_right_center {
                width: 100%;
                height: 35px;
                color: #a3a3a3;
                font-size: 12px;
                display: flex;
                align-items: center;

                .contract {
                    div {
                        padding: 5px 10px;
                        box-sizing: border-box;
                        border-radius: 13px;
                        line-height: 1;
                    }
                }

                .contract_break {
                    margin-left: 15px;

                    div {
                        padding: 5px 10px;
                        box-sizing: border-box;
                        border-radius: 13px;
                        line-height: 1;
                    }
                }

                span {
                    font-size: 11px;
                    display: inline-block;
                    color: #6b6b6b;
                    padding: 2px 4px;
                }
            }

            .div_right_bottom {
                display: flex;
                width: 100%;
                align-items: center;
                justify-content: space-between;
                font-size: 12px;

                .div_right_bottom_div {
                    display: flex;
                    align-items: center;

                    .label {
                        width: 64px;
                        height: 22px;
                        background: #FFFFFF;
                        border-radius: 2px;
                        border: 1px solid #0094FF;
                        font-weight: 400;
                        font-size: 11px;
                        color: #0094FF;
                        text-align: center;
                        line-height: 22px;
                        box-sizing: border-box;
                    }

                    .value {
                        width: 40px;
                        text-align: center;
                        line-height: 22px;
                        height: 22px;
                        background: #EBF4FF;
                        border-radius: 2px;
                        font-weight: 500;
                        font-size: 12px;
                        color: #0094FF;
                    }
                }
            }
        }
    }
}
</style>